package com.zwh.rocketmq.demo.order;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

/**
 * 顺序消费者，
 *
 * @author zwh
 * @date 2024-1-19 10:39
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class OrderMessageConsumer implements RocketMQListener<String> {

    @Override
    public void onMessage(String message) {
        log.info("监听到顺序消息：msg={}", message);
    }

    /**
     * 消费模式要选择orderly方式，保证用单线程来消费队列的消息。
     *  默认的consumeMode=ConsumeMode.CONCURRENTLY 多线程并发的方式来消费队列消息，不能保证有序。
     */
    @Service
    @RocketMQMessageListener(
            topic = "TEST_ORDER_TOPIC",
            consumerGroup = "ORDER_GROUP",
            consumeMode = ConsumeMode.ORDERLY
    )
    public class OrderConsumer1 implements RocketMQListener<String> {
        // 监听到消息就会执行此方法
        @Override
        public void onMessage(String message) {
            log.info("顺序消费者1：接收到消息：msg={}", message);
        }
    }

    @Service
    @RocketMQMessageListener(
            topic = "TEST_ORDER_TOPIC",
            consumerGroup = "ORDER_GROUP",
            consumeMode = ConsumeMode.ORDERLY
    )
    public class OrderConsumer2 implements RocketMQListener<String> {
        // 监听到消息就会执行此方法
        @Override
        public void onMessage(String message) {
            log.info("顺序消费者2：接收到消息：msg={}", message);
        }
    }
}
